var area = require('../../data/area')
const app = getApp()
var p = 0, c = 0, d = 0
Page({
  data:{
    provinceName: [],       // 省名
    provinceCode: [],       //省代码
    provinceSelIndex: '',   //省塞尔指数
    cityName: [],           //城市名称
    cityCode: [],           //城市代码
    citySelIndex: '',       //城市塞尔指数
    districtName: [],       //地区名称
    districtCode: [],       //地区代码
    districtSelIndex: '',   //区赛指数
    showMessage: false,     //显示信息：假的，
    messageContent: '',     // 信件内容
    showDistpicker: false,   //显示增强器：假
    tel: '',
    name:'',
    address:"14D",
    addressList: '',         //用户地址列表
  },
  onLoad:function(options){
    // 载入时要显示再隐藏一下才能显示数据，如果有解决方法可以在issue提一下，不胜感激:-)
    // 初始化数据
    this.setAreaData()
    //用户地址列表
    this.addressList()
  },
  setAreaData: function(p, c, d){
    var p = p || 0 // provinceSelIndex
    var c = c || 0 // citySelIndex
    var d = d || 0 // districtSelIndex
    // 设置省的数据
    var province = area['100000']
    var provinceName = [];
    var provinceCode = [];
    for (var item in province) {
      provinceName.push(province[item])
      provinceCode.push(item)
    }
    this.setData({
      provinceName: provinceName,
      provinceCode: provinceCode
    })
    // 设置市的数据
    var city = area[provinceCode[p]]
    var cityName = [];
    var cityCode = [];
    for (var item in city) {
      cityName.push(city[item])
      cityCode.push(item)
    }
    this.setData({
      cityName: cityName,
      cityCode: cityCode
    })
    // 设置区的数据
    var district = area[cityCode[c]]
    var districtName = [];
    var districtCode = [];
    for (var item in district) {
      districtName.push(district[item])
      districtCode.push(item)
    }
    this.setData({
      districtName: districtName,
      districtCode: districtCode
    })
  },
  changeArea: function(e) {
    p = e.detail.value[0]
    c = e.detail.value[1]
    d = e.detail.value[2]
    this.setAreaData(p, c, d)
  },
  showDistpicker: function() {
    this.setData({
      showDistpicker: true
    })
  },
  distpickerCancel: function() {
    this.setData({
      showDistpicker: false
    })
  },
  distpickerSure: function() {
    this.setData({
      provinceSelIndex: p,
      citySelIndex: c,
      districtSelIndex: d
    })
    this.distpickerCancel()
  },
  savePersonInfo: function(e) {
    var data = e.detail.value
    var telRule = /^1[3|4|5|7|8]\d{9}$/, nameRule = /^[\u2E80-\u9FFF]+$/
    if (data.name == '') {
      this.showMessage('请输入姓名')
    } else if (! nameRule.test(data.name)) {
      this.showMessage('请输入中文名')
    } else if (data.tel == '') {
      this.showMessage('请输入手机号码')
    } else if (! telRule.test(data.tel)) {
      this.showMessage('手机号码格式不正确')
    } else if (data.province == '') {
      this.showMessage('请选择所在地区')
    } else if (data.city == '') {
      this.showMessage('请选择所在地区')
    } else if (data.district == '') {
      this.showMessage('请选择所在地区')
    } else if (data.address == '') {
      this.showMessage('请输入详细地址')
    } else {
      this.showMessage(' 保存成功')
      console.log(data)
      console.log(data.name + data.tel)
      console.log(data.province + data.city + data.district + data.address)
      // this.setData({
      //   name: data.name,
      //   tel: data.tel,
      //   provinceName: data.province,
      //   cityName: data.city,
      //   districtName: data.district,
      //   address:  data.address
      // })
      //添加用户地址
      wx.request({
        url: app.globalData.apiUrl+'/api/User/addAddress',
        data: {users_id:app.globalData.userInfo.id,linkMan:data.name,mobile:data.tel,area:data.city+data.district,address:data.address,
        status:data.default},
        method: 'POST',
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.status==1){
            wx.showToast({
              title:'新增地址成功!',
            })
          }
        },
        fail: function(res) {},
        complete: function(res) {},
      })
      wx.navigateTo({
        url: '../zhifu/zhifu',
      })
     console.log(this.data.name)
    }
  },
  showMessage: function(text) {
    var that = this
    that.setData({
      showMessage: true,
      messageContent: text
    })
    setTimeout(function(){
      that.setData({
        showMessage: false,
        messageContent: ''
      })
    }, 3000)
  },
  /**
   * 显示地址列表
   * 
   */
  addressList: function (){
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/api/User/selectAddress',
      data: {users_id:app.globalData.userInfo.id},
      method: 'GET',
      dataType: 'json',
      success: function(res) {
          that.setData({addressList:res.data})
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 改变默认的地址
   */
  changeStatus: function (e){
    var that = this;
    console.log(2)
    var id = e.currentTarget.dataset.id//地址id
    wx.request({
      url: app.globalData.apiUrl + '/api/User/selectTap',
      data: {id:id},
      method: 'GET',
      dataType: 'json',
      success: function(res) {
          that.addressList()
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 删除地址
   */
  delAddress: function(tab){
    var that = this
    //地址id
    var id = tab.currentTarget.dataset.id
    wx.request({
      url: app.globalData.apiUrl + '/api/User/delAddress',
      data: {id:id},
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        if(res.data.status==1){
          that.addressList()
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })

  }
})